package TreeHole.mod;

import TreeHole.patches.TreeHolePatch;
import TreeHole.save.TreeHoleSave;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.random.Random;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.saveAndContinue.SaveFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class TreeHoleHelper {
    private static final HashMap<String, TreeHoleBase> map = new HashMap<>();

    public static boolean isDungeonSameID(String dungeonID){
        if(AbstractDungeon.id == null){
            return false;
        }
        return AbstractDungeon.id.startsWith(dungeonID);
    }

    public static void addTreeHole(String ID, TreeHoleBase base){
        map.put(ID,base);
        TreeHolePatch.hasModAdded = true;
    }

    public static TreeHoleBase getTreeHole(String ID){
        return map.get(ID);
    }

    public static int getCurrentType(){
        return TreeHolePatch.currentType;
    }

    public static int getModSize(){
        return map.size();
    }

    public static int getSharedEventSize(){
        int size = 0;
        for (TreeHoleBase base:map.values()){
            if(base.enableSharedTreeHoleEvent())
                size++;
        }
        return size;
    }

    public static void enterTreeHole(String ID ,int holeType){
        TreeHolePatch.enterTreeHole(ID,holeType);
    }

    public static void outerTreeHole(){
        TreeHolePatch.outerTreeHole();
    }

    public static boolean hasEnteredCurrentRoom(){
        return TreeHolePatch.enteredTreeHole(AbstractDungeon.getCurrMapNode().x,AbstractDungeon.getCurrMapNode().y);
    }

    public static ArrayList<String> getIDs(){
        return new ArrayList<>(map.keySet());
    }

    public static ArrayList<String> getSharedIDs(){
        ArrayList<String> ids = new ArrayList<>(map.keySet());
        Iterator<String> it = ids.iterator();
        while(it.hasNext()){
            String ID = it.next();
            TreeHoleBase base = map.get(ID);
            if(!base.enableSharedTreeHoleEvent())
                it.remove();
        }
        return ids;
    }

    public static boolean contains(String ID){
        for(String key:map.keySet()){
            if(key.equals(ID))
                return true;
        }
        return false;
    }

    public static AbstractDungeon getDungeon(String ID, AbstractPlayer p, ArrayList<String> theList,int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            return base.getDungeon(p,theList,currentType);
        }
        return null;
    }

    public static AbstractDungeon getDungeon(String ID, AbstractPlayer p, SaveFile saveFile,int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            return base.getDungeon(p,saveFile,currentType);
        }
        return null;
    }


    public static void triggerWhenEnterTreeHole(String ID,int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            base.triggerWhenEnterTreeHole(currentType);
        }
    }

    public static boolean isBoss(String ID,int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            return base.isBoss(currentType);
        }
        return true;
    }

    public static int getBossEnterY(String ID, int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            return base.getBossEnterY(currentType);
        }
        return 15;
    }

    public static String getLevelNum(String ID,int currentType){
        TreeHoleBase base = map.get(ID);
        if(base!=null){
            return base.getLevelNum(currentType);
        }
        return "";
    }

    public static String getLevelName(String ID,int currentType){
        TreeHoleBase base  = map.get(ID);
        if (base!=null){
            return base.getLevelName(currentType);
        }
        return "";
    }

    public static float getMapSize(String ID,int currentType){
        TreeHoleBase base = map.get(ID);
        if (base!=null){
            return base.getMapSize(currentType);
        }
        return -560* Settings.scale;
    }

    public static float getScrollLimit(String ID,int currentType){
        TreeHoleBase base = map.get(ID);
        if (base!=null){
            return base.getScrollLimit(currentType);
        }
        return -350F* Settings.scale;
    }

    public static ArrayList<String> getEnterOptions(){
        ArrayList<String> options = new ArrayList<>();
        for(String id:getSharedIDs()){
            TreeHoleBase base = map.get(id);
            options.add(base.getEnterOption());
        }
        return options;
    }

    public static AbstractEvent treeHoleEnterEvent(String ID, Random rng){
        TreeHoleBase base  = map.get(ID);
        if (base!=null){
            return base.treeHoleEnterEvent(rng);
        }
        return null;
    }

    public static AbstractEvent treeHoleOuterEvent(String ID, Random rng){
        TreeHoleBase base  = map.get(ID);
        if (base!=null){
            return base.treeHoleOuterEvent(rng);
        }
        return null;
    }

    public static AbstractEvent getOnlyEvent(Random rng){
        for(String id:getSharedIDs()){
            TreeHoleBase base = map.get(id);
            return base.treeHoleEnterEvent(rng);
        }
        return null;
    }

    public static boolean anySharedEventEnabled(){
        for (TreeHoleBase base:map.values()){
            if(base.enableSharedTreeHoleEvent())
                return true;
        }
        return false;
    }

    public static boolean anyEndingEnabled(){
        for(TreeHoleBase base:map.values()){
            if(base.enableTheEndingTreeHole())
                return true;
        }
        return false;
    }

    public static ArrayList<TreeHoleBase> getAllBases(){
        return new ArrayList<>(map.values());
    }

}
